package cn.skylark.wcm.common.splitter.doc;

import org.jsoup.Jsoup;

import java.io.*;

public class Util {

    private static String os = "Windows";
    public static String wcmserver = "http://publictest-1253363526.file.myqcloud.com";

    //remove the invalid charactors from filename;
    public static String generateFilename(String filename) {
        String processedfilename;
        processedfilename = filename.replaceAll("[\\\\/:*?\"<>|]", "");
        return processedfilename.trim();

    }

    public static String html2text(String html) {
        return Jsoup.parse(html).text();
    }

    public static String removedocextension(String doc) {
        File file = new File(doc);
        if (file.getName().indexOf("doc") != -1) {
            String filename = file.getName().substring(0, file.getName().indexOf("doc"));
            return file.getParent() + File.separator + filename;
        } else {
            return null;
        }
    }

    public static String removedocfilextension(File file) {
        if (file.getName().indexOf("doc") != -1) {
            //System.out.println("filename:"+file.getName()+" doc position"+file.getName().indexOf("doc"));
            String filename = file.getName().substring(0, file.getName().indexOf("doc"));
            return file.getParent() + "/" + filename;
        } else {
            return null;
        }
    }

    public static void writeFile(String content, String path, boolean append) {
        FileOutputStream output = null;
        BufferedWriter bw = null;
        try {
            File file = new File(path);
            output = new FileOutputStream(file, append);
            //bw = new BufferedWriter(new OutputStreamWriter(output,"GB2312"));  
            bw = new BufferedWriter(new OutputStreamWriter(output));
            bw.write(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();
                if (output != null)
                    output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
